/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.nestedConfigs;

import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspAssociation;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import lombok.var;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;

public class SimpleUdtMappingGenerator extends NestedMappingGenerator {

  private SimpleDataTypeDef getUdt() {
    return (SimpleDataTypeDef) udt;
  }

  public SimpleUdtMappingGenerator(
      IGspCommonDataType dataType,
      IGspCommonField field,
      SimpleDataTypeDef udt,
      DataTypeDataGeneratorContext dataGenContext) {
    super(dataType, field, udt, dataGenContext);
  }

  @Override
  protected org.eclipse.jdt.core.dom.Block buildGetMethodBody() {
    java.util.HashMap<String, String> dict = new java.util.HashMap<String, String>();
    switch (getUdt().getObjectType()) {
      case None:
        dict.put(field.getLabelID(), getUdt().getCode());
        break;
      case Association:
        addAssUdtMapping(dict);
        break;
      case Enum:
        dict.put(field.getLabelID(), getUdt().getCode());
        // AddAssUdtMapping(dict);
        break;
      default:
        break;
    }

    return getMappingBolck(dict);
  }

  private void addAssUdtMapping(java.util.HashMap<String, String> mapping) {

    var fieldAssociation = field.getChildAssociations().get(0);

    var udtAssociation = getUdt().getChildAssociations().get(0);
    mapping.put(field.getLabelID(), getUdt().getCode());

    for (var item : fieldAssociation.getRefElementCollection()) {
      if (!item.getIsFromAssoUdt()) {
        continue;
      }
      IGspCommonField udtRefField =
          getElementByRefElementID(udtAssociation, item.getRefElementId(), item.getLabelID());
      mapping.put(item.getLabelID(), udtRefField.getLabelID());
    }
  }

  private IGspCommonField getElementByRefElementID(
          GspAssociation udtAssociation, String refElementID, String labelId) {

    for (var item : udtAssociation.getRefElementCollection()) {
      if (refElementID.equals(item.getRefElementId())) {
        return item;
      }
    }
    throw new RuntimeException(
        "找不到UDT[" + getUdt().getCode() + "]关联字段" + refElementID + "----" + labelId);
  }

}
